package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.OutcomeManage;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.OutcomeManageMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IOutcomeManageService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

@Service
public class OutcomeManageServiceImpl implements IOutcomeManageService {
    @Autowired
    private OutcomeManageMapper outcomeManageMapper;


    @Override
    public void saveOrUpdate(OutcomeManage entity) {
        if (entity.getId()!=null){
            outcomeManageMapper.updateByPrimaryKey(entity);
        }else {
            outcomeManageMapper.insert(entity);
        }
    }

    @Override
    public OutcomeManage selectByPrimaryKey(Long id) {
        return outcomeManageMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OutcomeManage> selectAll() {
        return outcomeManageMapper.selectAll();
    }




    public PageResult quey4List(QueryObject qo) {
        Integer total = outcomeManageMapper.query4Count(qo);
        if (total == 0) {
            return PageResult.PAGERESULT_EMPTY;
        }
        return new PageResult(total, outcomeManageMapper.query4List(qo));
    }




    /**
     * 根据条件查询支出列表导出到exl
     * @param qo
     * @return
     */
    @Override
    public Workbook exportXls(QueryObject qo) {
        //创建xls
        Workbook book = new HSSFWorkbook();
        //创建工作簿
        Sheet sheet = book.createSheet();
        //设置第一行的标题
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("日期");
        row.createCell(1).setCellValue("支付金额");
        row.createCell(2).setCellValue("摘要");
        row.createCell(3).setCellValue("支出人");
        row.createCell(4).setCellValue("经手人");
        row.createCell(5).setCellValue("支付方式");
        row.createCell(6).setCellValue("花费类型");
        row.createCell(7).setCellValue("单据号");
        row.createCell(8).setCellValue("所属班级");
        row.createCell(9).setCellValue("审核人");
        row.createCell(10).setCellValue("状态");

        List<OutcomeManage> list = (List<OutcomeManage>) outcomeManageMapper.query4List(qo);

        for(int i=0;i<list.size();i++){
            try {
                OutcomeManage out = (OutcomeManage) list.get(i);
                //创建行
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(out.getHiredate());
                row.createCell(1).setCellValue(out.getPayMoney().toString());
                row.createCell(2).setCellValue(out.getDigest());
                row.createCell(3).setCellValue(out.getPayUser()!=null?out.getPayUser().getUsername():"");
                row.createCell(4).setCellValue(out.getHandlerUser()!=null?out.getHandlerUser().getUsername():"");
                row.createCell(5).setCellValue(out.getPayType()!=null?out.getPayType().getName():"");
                row.createCell(6).setCellValue(out.getPayStyle()!=null?out.getPayStyle().getName():"");
                row.createCell(7).setCellValue(out.getReceipt());
                row.createCell(8).setCellValue(out.getClz()!=null?out.getClz().getName():"");
                row.createCell(9).setCellValue(out.getAuditor()!=null?out.getAuditor().getUsername():"");
                row.createCell(10).setCellValue(out.getState());

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return book;
    }

    /**
     * auditorId
     * 根据shiro获取当前的用户进行审核人
     * 根据id删除支出
     * @param id
     */
    public void delete(Long id) {
        outcomeManageMapper.deleteByPrimaryKey(id);
    }

    //审核支出明细
    public void auditor(Long id) {
        Long auditorId = null;
        OutcomeManage outcomeManage = outcomeManageMapper.selectByPrimaryKey(id);
        if (outcomeManage.getState() ==null || !outcomeManage.getState()){

            Employee auditor = (Employee) SecurityUtils.getSubject().getPrincipal();
            auditorId = auditor.getId();
        }
        outcomeManageMapper.auditorById(auditorId,id);
    }
}
